<?php

namespace app\api\controller\v1;

use addons\litestore\model\Area as AddArea;
use app\admin\model\Userweixin;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Area;
use EasyWeChat\Factory;
use fast\Http;
use fast\Random;
use think\Validate;

/**
 * 用户地址
 */
class Useraddress extends Api
{
    protected $noNeedLogin = ['confidence'];
    protected $noNeedRight = '*';
    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

        $config = [
            'app_id' => 'wx24f19287ae7f5d5e',
            'secret' => '4d76532cde1d11b0e03054083eddea4b',

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }

    public function all()
    {
        $user = $this->auth->getUser();
        $userAddressModel = new \app\admin\model\Useraddress();
        $keyword = $this->request->param('keyword');
        $where = array();
        $where['uid'] = $user['id'];
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['mobile'] = array('like', '%' . $keyword . '%');

            } else {
                $where['username'] = array('like', '%' . $keyword . '%');

            }
        }
        $list = $userAddressModel->where($where)->select();
        foreach ($list as $k => $v) {
            $list[$k]['provinceName'] = AddArea::getNameById($v['provinceid']);
            $list[$k]['cityName'] = AddArea::getNameById($v['cityid']);
            $list[$k]['areaName'] = AddArea::getNameById($v['areaid']);

        }
        $this->success('', $list);

    }

    public function getDefault()
    {
        $user = $this->auth->getUser();

        $userAddressModel = new \app\admin\model\Useraddress();
        $one = $userAddressModel->where('uid', $user['id'])->where('isdefault', 1)->find();
        if ($one) {
            $one['provinceName'] = AddArea::getNameById($one['provinceid']);
            $one['cityName'] = AddArea::getNameById($one['cityid']);
            $one['areaName'] = AddArea::getNameById($one['areaid']);
        }
        $this->success('', $one);

    }

    public function add()
    {

        $username = $this->request->param('username');
        $mobile = $this->request->param('mobile');
        $provinceId = $this->request->param('provinceId');
        $areaId = $this->request->param('areaId');
        $cityId = $this->request->param('cityId');
        $address = $this->request->param('address');

        $user = $this->auth->getUser();
        $userAddressModel = new \app\admin\model\Useraddress();
        $list = $userAddressModel->where('uid', $user['id'])->select();
        $isDefault = 1;
        if ($list) {
            foreach ($list as $key => $val) {
                $userAddressModel->where('id', $val['id'])->update(['isdefault' => 0]);
            }
        }
        $id = $userAddressModel->insertGetId(['username' => $username, 'mobile' => $mobile,
            'provinceid' => $provinceId, 'areaid' => $areaId, 'cityid' => $cityId,
            'address' => $address, 'isdefault' => $isDefault, 'uid' => $user['id']]);
        if ($id) {
            $this->success('成功');
        } else {
            $this->success('添加失败');
        }


    }

    public function edit()
    {
        $username = $this->request->param('username');
        $mobile = $this->request->param('mobile');
        $provinceId = $this->request->param('provinceId');
        $areaId = $this->request->param('areaId');
        $cityId = $this->request->param('cityId');
        $address = $this->request->param('address');
        $isdefault = $this->request->param('isdefault');

        $userAddressModel = new \app\admin\model\Useraddress();
        $user = $this->auth->getUser();

        $list = $userAddressModel->where('uid', $user['id'])->select();
        $isDefault = 1;
        if ($list) {
            foreach ($list as $key => $val) {
                $userAddressModel->where('id', $val['id'])->update(['isdefault' => 0]);
            }
        }

        $id = $this->request->param('id');
        $userAddressModel = new \app\admin\model\Useraddress();
        $one = $userAddressModel->where('id', $id)->find();
        if ($one) {

            if ($address) $one->address = $address;
            if ($provinceId) $one->provinceid = $provinceId;
            if ($cityId) $one->cityid = $cityId;
            if ($mobile) $one->mobile = $mobile;
            if ($username) $one->username = $username;
            if ($areaId) $one->areaid = $areaId;
            $one->isdefault = 1;
            $one->save();
            $this->success('修改成功');

        } else {
            $this->error('记录不存在');

        }
    }

    public function del()
    {

        $id = $this->request->param('id');
        $userAddressModel = new \app\admin\model\Useraddress();
        $userAddressModel->where('id', $id)->delete();
        $this->success('删除成功');
    }

    public function setDefault()
    {

        $user = $this->auth->getUser();
        $userAddressModel = new \app\admin\model\Useraddress();
        $id = $this->request->param('id');

        $userAddressModel->where('uid', $user['id'])->update(['isdefault' => 0]);
        $userAddressModel->where('id', $id)->update(['isdefault' => 1]);

        $this->success('成功');
    }


    //地址识别
    public function confidence()
    {


        import('aip-php-sdk.AipNlp', EXTEND_PATH, '.php');

        $APP_ID = '23900907';
        $API_KEY = 'GQhlzAWU2ojv6CBypkYbzl7e';
        $SECRET_KEY = 'tjFVEjNsXugOexZi9tZWLHe9BbU5GRP5';

        $client = new \AipNlp($APP_ID, $API_KEY, $SECRET_KEY);
        $inText = $this->request->param('inText');
        if (!$inText) {
            $this->error('请输入你要识别的地址');
        }
        $ret = $client->address($inText);
        //$ret = json_decode($out,true);
        $province = $ret['province'];
        $city = $ret['city'];
        $county = $ret['county'];
        $detail = $ret['detail'];
        $person = $ret['person'];
        $phonenum = $ret['phonenum'];

        $data = array();
        $data['username'] = $person;
        $data['mobile'] = $phonenum;
        $data['province'] = $province;
        $data['city'] = $city;
        $data['county'] = $county;
        $data['detail'] = $detail;

        $areaModel = new Area();
        $provDb = $areaModel->where('name', $province)->find();
        if ($provDb) {
            $data['provinceId'] = $provDb['id'];
        }
        $cityDb = $areaModel->where('name', $city)->find();
        if ($cityDb) {
            $data['cityId'] = $cityDb['id'];
        }
        $countyDb = $areaModel->where('name', $county)->find();
        if ($countyDb) {
            $data['areaId'] = $countyDb['id'];
        }
        $this->success('', $data);


    }

}
